package com.hdcircles.autoform.util;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.EditText;

import com.hdcircles.autoform.app.App;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.SimpleFormatter;

/**
 * Created by hdcircles on 2016/9/21.
 */

public class SystemUtil {

    /**
     * 检查WIFI是否连接
     */
    public static boolean isWifiConnected(){
        ConnectivityManager connectivityManager= (ConnectivityManager) App.getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return wifiInfo!=null;
    }

    /**
     * 检查收集网络(4G/3G/2G)是否连接
     */
    public static boolean isMobileNetWorkConnected(){
        ConnectivityManager connectivityManager= (ConnectivityManager) App.getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileNetworkInfo =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return mobileNetworkInfo !=null;
    }

    /**
     * 检查是否有可用网络
     */
    public static boolean isNetWorkConnected(){
        ConnectivityManager connectivityManager= (ConnectivityManager) App.getInstance().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo()!=null;
    }

    public static String long2Date(long time){
        Date date=new Date(time);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
        return dateFormat.format(date);
    }

    public static String genID(long time){
        Date date=new Date(time);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        return dateFormat.format(date);
    }



    /**
     * 保存文字到剪贴板
     */
    public static void copy2ClipBoard(Context context,String text){
        ClipData clipData=ClipData.newPlainText("url",text);
        ClipboardManager manager= (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        manager.setPrimaryClip(clipData);
        ToastUtil.shortShow("已复制到剪贴板");
    }

    /**
     * dp 2 px
     */
    public static int dp2px(Context context,float dp){
        final float scale=context.getResources().getDisplayMetrics().density;
        return (int)(dp*scale+0.5f);
    }

    /**
     * px 2 dp
     */
    public static int px2dp(Context context,float px){
        final float scale=context.getResources().getDisplayMetrics().density;
        return (int)(px/scale+0.5f);
    }

    /**
     * 判断edittext是否null
     */
    public static String checkEditText(EditText editText) {
        if (editText != null && editText.getText() != null
                && !(editText.getText().toString().trim().equals(""))) {
            return editText.getText().toString().trim();
        } else {
            return "";
        }
    }

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static <T> Map<String, T> sortMapByKey(Map<String, T> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, T> sortMap = new TreeMap<String, T>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }

    private static class MapKeyComparator implements Comparator<String> {

        private static Integer intValue(String s) {
            try {
                return Integer.valueOf(s);
            } catch (NumberFormatException e) {
                return null;
            }
        }

        @Override
        public int compare(String s1, String s2) {
            Integer i1 = intValue(s1);
            Integer i2 = intValue(s2);
            if (i1 == null && i2 == null) {
                return s1.compareTo(s2);
            } else if (i1 == null) {
                return -1;
            } else if (i2 == null) {
                return 1;
            } else {
                return i1.compareTo(i2);
            }
        }
    }




}
